<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="购物车id" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入购物车id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品id" prop="goodsid">
        <el-input
          v-model="queryParams.goodsid"
          placeholder="请输入商品id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品数量" prop="nums">
        <el-input
          v-model="queryParams.nums"
          placeholder="请输入商品数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userid">
        <el-input
          v-model="queryParams.userid"
          placeholder="请输入用户id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="删除标志" prop="del">
        <el-select v-model="queryParams.del" placeholder="请选择删除标志" clearable>
          <el-option
            v-for="dict in del"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['dianshang:cart:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dianshang:cart:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dianshang:cart:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['dianshang:cart:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cartList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="购物车id" align="center" prop="id" />
      <el-table-column label="商品id" align="center" prop="goodsid" />
      <el-table-column label="商品数量" align="center" prop="nums" />
      <el-table-column label="用户id" align="center" prop="userid" />
      <el-table-column label="删除标志" align="center" prop="del">
        <template #default="scope">
          <dict-tag :options="del" :value="scope.row.del"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['dianshang:cart:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['dianshang:cart:remove']">删除</el-button>
          <el-button link type="primary" @click="handleXiangQing(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购物车对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="cartRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品id" prop="goodsid">
          <el-input v-model="form.goodsid" placeholder="请输入商品id" />
        </el-form-item>
        <el-form-item label="商品数量" prop="nums">
          <el-input v-model="form.nums" placeholder="请输入商品数量" />
        </el-form-item>
        <el-form-item label="用户id" prop="userid">
          <el-input v-model="form.userid" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="删除标志" prop="del">
          <el-select v-model="form.del" placeholder="请选择删除标志">
            <el-option
              v-for="dict in del"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

        <!-- 购物车详情展示框 -->
        <el-dialog title="购物车订单详细信息" v-model="dialogVisible" width="50%">
      <el-descriptions title="用户信息" :column="2">
        <el-descriptions-item label="昵称">{{ user.nickName }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ user.phonenumber }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ user.email }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="商品信息" :column="2">
        <el-descriptions-item label="商品名称">{{ goods.name }}</el-descriptions-item>
        <el-descriptions-item label="商品价格">{{ goods.money }}元</el-descriptions-item>
        <el-descriptions-item label="提货日期">{{ goods.time }}</el-descriptions-item>
        <el-descriptions-item label="上架时间">{{ goods.creat }}</el-descriptions-item>
        <el-descriptions-item label="商品类型">{{ goods.leixing }}</el-descriptions-item>
        <el-descriptions-item label="商品规格">{{ goods.gueige }}</el-descriptions-item>
        <el-descriptions-item label="详细介绍">{{ goods.text }}</el-descriptions-item>
        <el-descriptions-item label="商品图片">
          <img :src="goods.image" alt="商品图片" width="100" />
        </el-descriptions-item>
        <el-descriptions-item label="销量">{{ goods.totalsale }}</el-descriptions-item>
      </el-descriptions>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script setup name="Cart">
import { listCart, getCart, delCart, addCart, updateCart } from "@/api/dianshang/cart";
import { getUser } from "@/api/system/user"
import { getGoods } from "@/api/dianshang/goods"


const { proxy } = getCurrentInstance();
const { del } = proxy.useDict('del');

const cartList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const user = ref({});
const goods = ref({});
const dialogVisible = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    goodsid: null,
    nums: null,
    userid: null,
    del: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询购物车列表 */
function getList() {
  loading.value = true;
  listCart(queryParams.value).then(response => {
    cartList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
function handleXiangQing(data) {
  console.log(data);
  getUser(data.userid).then(response => {
    user.value = response.data; 
    getGoods(data.goodsid).then(response1 => {
      console.log(response1);
      
      goods.value = response1.data;
      dialogVisible.value = true;
    })
  })
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    goodsid: null,
    nums: null,
    userid: null,
    del: null
  };
  proxy.resetForm("cartRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加购物车";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getCart(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改购物车";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["cartRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateCart(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCart(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除购物车编号为"' + _ids + '"的数据项？').then(function() {
    return delCart(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('dianshang/cart/export', {
    ...queryParams.value
  }, `cart_${new Date().getTime()}.xlsx`)
}

getList();
</script>
